package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 根据条件查询护理等级
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    public PageResponse<NursingLevelVo> listByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> page = nursingLevelMapper.listByPage(name, status);
        PageResponse<NursingLevelVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 新增护理等级
     * @param nursingLevelDto
     */
    public void insert(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class);
        nursingLevelMapper.insert(nursingLevel);
    }

    /**
     * 根据id查询护理等级
     * @param id
     * @return
     */
    public NursingLevelVo getById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.getById(id);
        return BeanUtil.toBean(nursingLevel, NursingLevelVo.class);
    }

    /**
     * 修改护理等级
     * @param nursingLevelDto
     */
    public void update(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class);
        nursingLevel.setUpdateTime(LocalDateTime.now());
        nursingLevelMapper.update(nursingLevel);
    }

    /**
     * 删除护理等级
     * @param id
     */
    public void delete(Long id) {
        //如果关联了护理计划，则不能删除
        NursingLevel nursingLevel = nursingLevelMapper.getById(id);
        if (nursingLevel.getPlanId() !=null){
            throw new RuntimeException("该护理等级关联了护理计划，不能删除!");
        }
        //如果状态启用中，则不能删除
        if (nursingLevel.getStatus()==1){
            throw new RuntimeException("状态启用中，不能删除!");
        }
        nursingLevelMapper.delete(id);
    }

    /**
     * 修改护理等级
     * @param id
     * @param status
     */
    public void updateStatus(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id,status);
    }

    /**
     * 查询所有护理等级
     * @return
     */
    public List<NursingLevelVo> listAll() {
        List<NursingLevelVo> nursingLevelList = nursingLevelMapper.listAll();
        return nursingLevelList;
    }
}
